﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class SetPanel : MonoBehaviour
{
    public Button open;
    public Button close;
    public Button back;
    public Button continueGame;
    public Button backMain;

    public Sprite Select;
    public Sprite NoSelect;
    

    private AudioSource ButtonAudios;

	// Use this for initialization
	void Start ()
    {
        ButtonAudios = GameObject.Find("Canvas").GetComponent<AudioSource>();

        if (SceneManager.GetActiveScene().name=="Login")
        {
            back.gameObject.SetActive(true);
            continueGame.gameObject.SetActive(false);
            backMain.gameObject.SetActive(false);
        }
        else
        {
            back.gameObject.SetActive(false);
            continueGame.gameObject.SetActive(true);
            backMain.gameObject.SetActive(true);
        }

        open.onClick.AddListener(OpenMusic);
        close.onClick.AddListener(CloseMusic);
        back.onClick.AddListener(Back);
        continueGame.onClick.AddListener(ContinueGame);
        backMain.onClick.AddListener(BackMain);
	}
	
    void OpenMusic()
    {
        ButtonAudios.Play();

        PlayerPrefs.SetInt("MusicNum",0);

        string name = SceneManager.GetActiveScene().name;
        switch (name)
        {
            case "Login":
                for (int i = 0; i < AudioControl.Instance.LoginAudio.Length; i++)
                {
                    AudioControl.Instance.LoginAudio[i].enabled = true;
                }
                break;
            case "001":
                for (int i = 0; i < AudioControl.Instance.Level1Audio.Length; i++)
                {
                    AudioControl.Instance.Level1Audio[i].enabled = true;
                }
                break;
            case "002":
                for (int i = 0; i < AudioControl.Instance.Level2Audio.Length; i++)
                {
                    AudioControl.Instance.Level2Audio[i].enabled = true;
                }
                break;
            case "003":
                for (int i = 0; i < AudioControl.Instance.Level3Audio.Length; i++)
                {
                    AudioControl.Instance.Level3Audio[i].enabled = true;
                }
                break;
            case "004":
                for (int i = 0; i < AudioControl.Instance.Level4Audio.Length; i++)
                {
                    AudioControl.Instance.Level4Audio[i].enabled = true;
                }
                break;
            case "005":
                for (int i = 0; i < AudioControl.Instance.Level5Audio.Length; i++)
                {
                    AudioControl.Instance.Level5Audio[i].enabled = true;
                }
                break;
            case "006":
                for (int i = 0; i < AudioControl.Instance.Level6Audio.Length; i++)
                {
                    AudioControl.Instance.Level6Audio[i].enabled = true;
                }
                break;
            case "007":
                for (int i = 0; i < AudioControl.Instance.Level7Audio.Length; i++)
                {
                    AudioControl.Instance.Level7Audio[i].enabled = true;
                }
                break;
            default:
                break;
        }

        open.gameObject.GetComponent<Image>().sprite = Select;
        close.gameObject.GetComponent<Image>().sprite = NoSelect;
       
    }

    void CloseMusic()
    {
        PlayerPrefs.SetInt("MusicNum", 1);

        string name = SceneManager.GetActiveScene().name;
        switch (name)
        {
            case "Login":
                for (int i = 0; i < AudioControl.Instance.LoginAudio.Length; i++)
                {
                    AudioControl.Instance.LoginAudio[i].enabled = false;
                }
                break;
            case "001":
                for (int i = 0; i < AudioControl.Instance.Level1Audio.Length; i++)
                {
                    AudioControl.Instance.Level1Audio[i].enabled = false;
                }
                break;
            case "002":
                for (int i = 0; i < AudioControl.Instance.Level2Audio.Length; i++)
                {
                    AudioControl.Instance.Level2Audio[i].enabled = false;
                }
                break;
            case "003":
                for (int i = 0; i < AudioControl.Instance.Level3Audio.Length; i++)
                {
                    AudioControl.Instance.Level3Audio[i].enabled = false;
                }
                break;
            case "004":
                for (int i = 0; i < AudioControl.Instance.Level4Audio.Length; i++)
                {
                    AudioControl.Instance.Level4Audio[i].enabled = false;
                }
                break;
            case "005":
                for (int i = 0; i < AudioControl.Instance.Level5Audio.Length; i++)
                {
                    AudioControl.Instance.Level5Audio[i].enabled = false;
                }
                break;
            case "006":
                for (int i = 0; i < AudioControl.Instance.Level6Audio.Length; i++)
                {
                    AudioControl.Instance.Level6Audio[i].enabled = false;
                }
                break;
            case "007":
                for (int i = 0; i < AudioControl.Instance.Level7Audio.Length; i++)
                {
                    AudioControl.Instance.Level7Audio[i].enabled = false;
                }
                break;
            default:
                break;
        }

        close.gameObject.GetComponent<Image>().sprite = Select;
        open.gameObject.GetComponent<Image>().sprite = NoSelect;
        
    }

    void Back()
    {
        ButtonAudios.Play();
        Destroy(gameObject);
    }

    void ContinueGame()
    {
        ButtonAudios.Play();
        Time.timeScale = 1;
        Destroy(gameObject);
    }

    void BackMain()
    {
        ButtonAudios.Play();
        SceneManager.LoadScene("Login");
    }
}
